//
//  YSTopicListCtrl.m
//  Yueshi
//
//  Created by yueshi－mac1 on 15/12/8.
//  Copyright © 2015年 悦食中国. All rights reserved.
//

#import "YSTopicListCtrl.h"
#import "YSClubDataTool.h"
#import "YSTopicItemFrame.h"
#import "YSTopicListRes.h"
#import "YSTopicItemCell.h"
#import "YSTopicListHeadeView.h"
#import "YSGroupIntroductionCtrl.h"
#import "YSReviewsCtrl.h"

@interface YSTopicListCtrl ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *itemArray;
@property (strong, nonatomic) NSMutableArray *allFrames;
//小组id
@property (strong, nonatomic) YSInterestGroup *interestGroup;
//页码
@property (assign, nonatomic) NSInteger pageIndex;

@property (strong ,nonatomic) UIButton *followBtn;

@property (strong, nonatomic) YSTopicListHeadeView *topicHeaderView;

@end

@implementation YSTopicListCtrl

- (instancetype)initWithInterestGroup:(YSInterestGroup *)interestGroup{
    self = [super init];
    if(self){
        _interestGroup = interestGroup;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:self.interestGroup.groupName];
    
    [self initRightTitle];
    
    _itemArray = [NSMutableArray array];
    _allFrames = [NSMutableArray array];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(praise:) name:kTopiclistPraiseNortif object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(comment:) name:kTopiclistCommentNortif object:nil];
    
    [self initTableView];
    
    [self reqeustData:0];
}

- (void) initRightTitle{
    if (!_followBtn) {
        UIButton *followBtn = [[UIButton alloc] init];
        followBtn.titleLabel.font = SYS_FONT(14);
        
        [followBtn.layer setMasksToBounds:YES];
        [followBtn.layer setCornerRadius:5]; //设置矩形四个圆角半径
        [followBtn.layer setBorderWidth:1.0]; //边框宽度
        
        [followBtn addTarget:self action:@selector(tapTitleRightItem:) forControlEvents:UIControlEventTouchUpInside];
        _followBtn = followBtn;
    }
    
    if (self.interestGroup.isAttention) {
        self.followBtn.frame = CGRectMake(0, 0, 55, 20);
        [self.followBtn setTitle:@"已关注" forState:UIControlStateNormal];
        [self.followBtn setTitleColor:TabarItemTextNormalColor forState:UIControlStateNormal];
        [self.followBtn.layer setBorderColor:[TabarItemTextNormalColor CGColor]];
    }else{
        self.followBtn.frame = CGRectMake(0, 0, 45, 20);
        [self.followBtn setTitle:@"关注" forState:UIControlStateNormal];
        [self.followBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
        [self.followBtn.layer setBorderColor:[ThemeColor CGColor]];
    }
    self.navigationItem.rightBarButtonItem =  [[UIBarButtonItem alloc]initWithCustomView:self.followBtn];
}

- (void) initTableView{
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.tableView setBackgroundColor:PUBLIC_BG];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    
    _topicHeaderView = [YSTopicListHeadeView headerView];
    [self.tableView setTableHeaderView:_topicHeaderView];
    _topicHeaderView.interestGroup = self.interestGroup;
    
    __weak YSTopicListCtrl *ctrl = self;
    _topicHeaderView.toGroupIntroduction = ^{
        //小组介绍
        [ctrl.navigationController pushViewController:[[YSGroupIntroductionCtrl alloc] initWithInterestGroup:ctrl.interestGroup] animated:YES];
    };

}

-(void)footerRefresh{
    self.pageIndex++;
    
    NSInteger lastId = 0;
    if(_itemArray.count){
        lastId = ((YSReviews *)[_itemArray lastObject]).id;
    }
    [self reqeustData:lastId];
}

-(void)tapTitleRightItem:(UIBarButtonItem *)btnItem{
    [YSClubDataTool attentionWithGroupId:self.interestGroup.groupId type:self.interestGroup.isAttention success:^(BaseResponse * result) {
        if (result.isSucc) {
            if (self.interestGroup.isAttention) {
                self.interestGroup.isAttention = 0;
            }else{
                self.interestGroup.isAttention = 1;
                ShowTips(@"关注成功");
            }
            
            [self initRightTitle];
        }else{
            ShowTips(result.data);
        }
    } failure:^(NSError *error) {
        ShowTips(kRequestFailure);
    }];
    
}

-(void)reqeustData:(NSInteger) lastId{
    [YSClubDataTool getTopicListWithGroupId:self.interestGroup.groupId pageIndex:self.pageIndex lastId:lastId success:^(YSTopicListRes *result) {
        [UIUtil refreshComplete:self.tableView];
        if (result.isSucc) {
            [self.itemArray addObjectsFromArray:result.reviews];
            [self itemArrayWithFrames];
        }else{
             ShowTips(result.data);
        }
    } failure:^(NSError *error) {
        [UIUtil refreshComplete:self.tableView];
        ShowTips(kRequestFailure);
    }];
}

-(void)itemArrayWithFrames{
    [_allFrames removeAllObjects];
    for (YSReviews *reviews in _itemArray) {
        YSTopicItemFrame *frame = [YSTopicItemFrame new];
        frame.reviews  = reviews;
        [_allFrames addObject:frame];
    }
    [_tableView reloadData];
}

#pragma - mark UITableViewDataSource,UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _allFrames.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YSTopicItemCell *cell = [YSTopicItemCell cellWithTableView:tableView];
    [cell setItemFrame:_allFrames[indexPath.row]];
    [cell setIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [_allFrames[indexPath.row] cellHeight];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    YSReviews *review = _itemArray[indexPath.row];
    YSReviewsCtrl *ctrl = [YSReviewsCtrl initWithReviews:review];
    [self.navigationController pushViewController:ctrl animated:YES];
}

#pragma mark notification method
-(void)praise:(NSNotification *)nortif{
    NSIndexPath *indexPath =  [nortif object];
    NSLog(@"praise index.row = %ld",indexPath.row);
    YSReviews *review = _itemArray[indexPath.row];
    
    [YSClubDataTool praiseWithReviewsId:review.id type:review.isLike success:^(BaseResponse * result) {
        if (result.isSucc) {
            NSInteger isLike = !review.isLike;
            review.isLike = isLike;
            [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        }else{
            ShowTips(result.data);
        }
    } failure:^(NSError *error) {
        ShowTips(kRequestFailure);
    }];
}

-(void)comment:(NSNotification *)nortif{
    NSIndexPath *indexPath =  [nortif object];
    NSLog(@"comment index.row = %ld",indexPath.row);
    
    YSReviews *review = _itemArray[indexPath.row];
    YSReviewsCtrl *ctrl = [YSReviewsCtrl initWithReviews:review];
    [self.navigationController pushViewController:ctrl animated:YES];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
